package main

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson/primitive"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

func main() {
	c := context.Background()

	mc, err := mongo.Connect(c, options.Client().ApplyURI("mongodb://admin:123456@localhost:27017/coolcar?authSource=admin&readPreference=primary&appname=mongodb-vscode%200.6.12&directConnection=true&ssl=false"))

	if err != nil {
		panic(err)
	}

	col := mc.Database("coolcar").Collection("account")

	//insertRows(c,col)
	findRows(c, col)

}

func findRows(c context.Context, collection *mongo.Collection) {
	one := collection.FindOne(c, bson.M{
		"open_id": "123",
	})

	var row struct {
		ID     primitive.ObjectID `bson:"_id"`
		OpenId string             `bson:"open_id"`
	}

	err := one.Decode(&row)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v\n", row)

}

func insertRows(c context.Context, collection *mongo.Collection) {
	res, err := collection.InsertMany(c, []interface{}{
		bson.M{
			"open_id": "123",
		},
		bson.M{
			"open_id": "456",
		},
	})

	if err != nil {
		panic(err)
	}

	fmt.Printf("%+v", res)
}
